package com.scsj.supermarket.view.activity.shopmodel;

import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.scsj.supermarket.R;
import com.scsj.supermarket.utils.SkipUtils;
import com.scsj.supermarket.view.activity.baseactivitymodel.BaseActivity;
import com.scsj.supermarket.view.fragment.basemodel.BaseFragment;
import com.scsj.supermarket.view.fragment.shopdetailmodel.ShopGoodsFragment;
import com.scsj.supermarket.view.fragment.shopdetailmodel.ShopHomeFragment;
import com.scsj.supermarket.view.fragment.shopdetailmodel.ShopPointsFragment;
import com.scsj.supermarket.view.fragment.shopdetailmodel.ShopShareFragment;
import com.vondear.rxtools.RxImageTool;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 店铺详情
 */
public class ShopActivity extends BaseActivity implements View.OnClickListener {
    private AppBarLayout layout_appbar;
    //顶部栏
    private LinearLayout back_ll, shop_find, classify_ll, menu_ll;
    private LinearLayout shop_details_ll, hot_classify_ll, service_ll;
    private LinearLayout tab_one, tab_two, tab_three, tab_four;
    LinearLayout[] tabs;
    private ImageView tab_one_ic;//tab 店铺首页
    private TextView tab_two_ic, tab_three_ic, tab_four_ic;
    private TextView tab_one_tv, tab_two_tv, tab_three_tv, tab_four_tv;
    TextView[] tab_tvs;
    private View tab_one_index, tab_two_index, tab_three_index, tab_four_index;
    View[] tab_indexs;
    private ImageView shop_follow;//关注
    //    private NavigationTabStrip mTopNavigationTabStrip;
    private ViewPager mViewPager;
    private ArrayList<BaseFragment> fragmentList;
    private PageAdapter mAdpter;
    private String[] titles = {"店铺首页", "全部商品", "积分购", "分享购"};

    @Override
    protected void createView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_shop);
        initToolbar();
    }

    @Override
    protected void initView() {
        initTabs();//初始化 tab
        initTabAnimat();//初始化 动画效果
        initNavigationBar();//初始化 ViewPager
        back_ll = findViewById(R.id.back_ll);//返回
        shop_find = findViewById(R.id.shop_find);//搜索本店
        classify_ll = findViewById(R.id.classify_ll);//分类
        menu_ll = findViewById(R.id.menu_ll);//菜单
        shop_follow = findViewById(R.id.shop_follow);
        shop_details_ll = findViewById(R.id.shop_details_ll);//店铺详情
        hot_classify_ll = findViewById(R.id.hot_classify_ll);//热门分类
        service_ll = findViewById(R.id.service_ll);//联系卖家

    }

    @Override
    protected void initListener() {
        back_ll.setOnClickListener(this);
        shop_find.setOnClickListener(this);
        classify_ll.setOnClickListener(this);
        menu_ll.setOnClickListener(this);
        shop_follow.setOnClickListener(this);
        shop_details_ll.setOnClickListener(this);
        hot_classify_ll.setOnClickListener(this);
        service_ll.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back_ll:
                finish();
                break;
            case R.id.shop_find:
                SkipUtils.toTotalGoods(this);
                break;
            case R.id.classify_ll:
                SkipUtils.toShopClassify(this);
                break;
            case R.id.shop_follow:
                //已关注
                shop_follow.setImageResource(R.mipmap.shop_follow_unselected);
                //未关注
//                shop_follow.setImageResource(R.mipmap.shop_follow_selected);
                break;
            case R.id.shop_details_ll://店铺详情
                SkipUtils.toShopDetail(this);
                break;
            case R.id.hot_classify_ll://热门分类

                break;
            case R.id.service_ll://联系卖家

                break;
            default:
                break;
        }
    }


    private void initNavigationBar() {

//        mTopNavigationTabStrip = (NavigationTabStrip) findViewById(R.id.mediator_tab);
        mViewPager = (ViewPager) findViewById(R.id.viewPager);
        /**
         * 设置预加载页面
         */
        mViewPager.setOffscreenPageLimit(1);

        ShopHomeFragment shopHomeFragment = new ShopHomeFragment();
        ShopGoodsFragment shopGoodsFragment = new ShopGoodsFragment();
        ShopPointsFragment shopPointsFragment = new ShopPointsFragment();
        ShopShareFragment shopShareFragment = new ShopShareFragment();
        fragmentList = new ArrayList<>();
        fragmentList.add(shopHomeFragment);
        fragmentList.add(shopGoodsFragment);
        fragmentList.add(shopPointsFragment);
        fragmentList.add(shopShareFragment);
        List localList = Arrays.asList(this.titles);
        mAdpter = new PageAdapter(getSupportFragmentManager(), fragmentList, localList);
        mViewPager.setAdapter(mAdpter);
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setTabstate(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        for (int i = 0; i < tabs.length; i++) {
            final int finalI = i;
            tabs[i].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mViewPager.setCurrentItem(finalI);
                    setTabstate(finalI);
                }
            });
        }
//        mTopNavigationTabStrip.setViewPager(mViewPager, 0);
//        mTopNavigationTabStrip.setTitles(titles);
//        mTopNavigationTabStrip.setStripType(NavigationTabStrip.StripType.LINE);
//        mTopNavigationTabStrip.setStripGravity(NavigationTabStrip.StripGravity.BOTTOM);
//        mTopNavigationTabStrip.setTabIndex(0, true);

    }

    private void initTabs() {
        //TAB
        tab_one = findViewById(R.id.tab_one);
        tab_two = findViewById(R.id.tab_two);
        tab_three = findViewById(R.id.tab_three);
        tab_four = findViewById(R.id.tab_four);
        tabs = new LinearLayout[]{tab_one, tab_two, tab_three, tab_four};
        //icon 图标 tab_one_ic 为 ImageView 其他为TextView
        tab_one_ic = findViewById(R.id.tab_one_ic);
        tab_two_ic = findViewById(R.id.tab_two_ic);
        tab_three_ic = findViewById(R.id.tab_three_ic);
        tab_four_ic = findViewById(R.id.tab_four_ic);
        //tab name
        tab_one_tv = findViewById(R.id.tab_one_tv);
        tab_two_tv = findViewById(R.id.tab_two_tv);
        tab_three_tv = findViewById(R.id.tab_three_tv);
        tab_four_tv = findViewById(R.id.tab_four_tv);
        tab_tvs = new TextView[]{tab_one_tv, tab_two_tv, tab_three_tv, tab_four_tv};
        //tab index
        tab_one_index = findViewById(R.id.tab_one_index);
        tab_two_index = findViewById(R.id.tab_two_index);
        tab_three_index = findViewById(R.id.tab_three_index);
        tab_four_index = findViewById(R.id.tab_four_index);
        tab_indexs = new View[]{tab_one_index, tab_two_index, tab_three_index, tab_four_index};
    }

    private void initTabAnimat() {
        layout_appbar = findViewById(R.id.layout_appbar);
        layout_appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            //verticalOffset是当前appbarLayout的高度与最开始appbarlayout高度的差，向上滑动的话是负数。
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                int imgHW = RxImageTool.dp2px(18);
                //如果折叠后固定，那么以下判断则是折叠完成时为true
                if (getSupportActionBar().getHeight() - appBarLayout.getHeight() == verticalOffset) {
                    //icon 图标缩小到无
                    tab_one_ic.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
                    tab_two_ic.setTextSize(0);
                    tab_two_ic.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
                    tab_three_ic.setTextSize(0);
                    tab_three_ic.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
                    tab_four_ic.setTextSize(0);
                    tab_four_ic.setLayoutParams(new LinearLayout.LayoutParams(0, 0));
                    for (int i = 0; i < tab_tvs.length; i++) {
                        tab_tvs[i].setTextSize(16);
                    }
                } else if (verticalOffset < 0 && getSupportActionBar().getHeight() - appBarLayout.getHeight() < verticalOffset) {
                    float a = getSupportActionBar().getHeight() - appBarLayout.getHeight();
                    float b = getSupportActionBar().getHeight() - appBarLayout.getHeight() - verticalOffset;
                    float scale = (float) b / a;
                    int img = (int) (imgHW * scale);
                    float iconText = 16 * scale;
                    float textSize = 16 - (16 - 12) * scale;
                    tab_one_ic.setLayoutParams(new LinearLayout.LayoutParams(img, img));
                    tab_two_ic.setTextSize(iconText);
                    tab_two_ic.setLayoutParams(new LinearLayout.LayoutParams(img, img));
                    tab_three_ic.setTextSize(iconText);
                    tab_three_ic.setLayoutParams(new LinearLayout.LayoutParams(img, img));
                    tab_four_ic.setTextSize(iconText);
                    tab_four_ic.setLayoutParams(new LinearLayout.LayoutParams(img, img));
                    for (int i = 0; i < tab_tvs.length; i++) {
                        tab_tvs[i].setTextSize(textSize);
                    }
                } else {
                    tab_one_ic.setLayoutParams(new LinearLayout.LayoutParams(imgHW, imgHW));
                    tab_two_ic.setTextSize(16);
                    tab_two_ic.setLayoutParams(new LinearLayout.LayoutParams(imgHW, imgHW));
                    tab_three_ic.setTextSize(16);
                    tab_three_ic.setLayoutParams(new LinearLayout.LayoutParams(imgHW, imgHW));
                    tab_four_ic.setTextSize(16);
                    tab_four_ic.setLayoutParams(new LinearLayout.LayoutParams(imgHW, imgHW));
                    for (int i = 0; i < tab_tvs.length; i++) {
                        tab_tvs[i].setTextSize(12);
                    }
                }
            }
        });
    }

    private void setTabstate(int finalI) {
        for (int i = 0; i < tab_indexs.length; i++) {
            if (finalI == i) {
                tab_tvs[i].setTextColor(getResources().getColor(R.color.price));
                tab_indexs[i].setVisibility(View.VISIBLE);
            } else {
                tab_tvs[i].setTextColor(getResources().getColor(R.color.main_black_text));
                tab_indexs[i].setVisibility(View.INVISIBLE);
            }
        }
        if (finalI != 0) {
            tab_one_ic.setImageResource(R.mipmap.shop_home_unselected);
            if (finalI == 1) {
                tab_two_ic.setTextColor(getResources().getColor(R.color.price));
            } else {
                tab_two_ic.setTextColor(getResources().getColor(R.color.main_black_text));
            }
            if (finalI == 2) {
                tab_three_ic.setTextColor(getResources().getColor(R.color.price));
            } else {
                tab_three_ic.setTextColor(getResources().getColor(R.color.main_black_text));
            }
            if (finalI == 3) {
                tab_four_ic.setTextColor(getResources().getColor(R.color.price));
            } else {
                tab_four_ic.setTextColor(getResources().getColor(R.color.main_black_text));
            }
        } else {
            tab_one_ic.setImageResource(R.mipmap.shop_home_selected);
            tab_two_ic.setTextColor(getResources().getColor(R.color.main_black_text));
            tab_three_ic.setTextColor(getResources().getColor(R.color.main_black_text));
            tab_four_ic.setTextColor(getResources().getColor(R.color.main_black_text));
        }
    }

    class PageAdapter extends FragmentPagerAdapter {
        List<BaseFragment> fragments;
        List<String> titles;

        public PageAdapter(FragmentManager manager, List<BaseFragment> paramList, List<String> paramList1) {
            super(manager);
            this.fragments = paramList;
            this.titles = paramList1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            //super.destroyItem(container, position, object);
        }

        @Override
        public void destroyItem(View container, int position, Object object) {
            // super.destroyItem(container, position, object);
        }

        public int getCount() {
            return this.fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
}
